package com.study.observermode.subject;

import com.study.observermode.observer.Observer;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: Subject
 * @Description:
 * @Details:
 * @Author: 郑港旭
 * @Create: 2021-11-16 14:45
 **/
public class Subject {
    private List<Observer> observers = new ArrayList<>();

    private Integer state;

    public List<Observer> getObservers() {
        return observers;
    }

    public void setObservers(List<Observer> observers) {
        this.observers = observers;
    }

    public Integer getState() {
        return state;
    }

    public void setState(Integer state) {
        this.state = state;
        notifyAllObservers();
    }

    public void attach(Observer observer){
        observers.add(observer);
    }

    private void notifyAllObservers(){
        for (Observer observer:
             observers) {
            observer.update();
        }
    }
}
